﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common.Command;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.Code
{
    // 输入地址：
    // ~/Interface/ajax/CommandService.aspx?key=ExampleApply&param={"Name":"123","Phone":"321"}
    // 返回结果：
    // {"ErrorCode":"0000","Error":false,"ErrorMsg":"unkown error","TotalCount":0,"Result":{"Addtime":"2014-02-20 12:45:30"}}

    /// <summary>
    /// ExampleApply命令参数
    /// </summary>
    public class ExampleApplyParameter : UserCommandParameter
    {
        public string Name { get; set; }
        public string Phone { get; set; }
    }

    /// <summary>
    /// ExampleApply命令调用输出
    /// </summary>
    public class ExampleApplyResult : CommandResult
    {
    }

    /// <summary>
    /// 返回的数据
    /// </summary>
    public class ExampleApplyResultData
    {
        public DateTime Addtime { get; set; }
    }

    /// <summary>
    /// ExampleApply命令
    /// </summary>
    public class ExampleApplyCommand : SessionCommand
    {
        public override CommandResult Execute(NHibernate.ISession session, CommandParameter parameter)
        {
            ExampleApplyResult result = new ExampleApplyResult();
            ExampleApplyParameter param = parameter as ExampleApplyParameter;

            if (param != null)
            {
                //在这里添加命令内容
                MessageInfo messageDb = new MessageInfo();
                messageDb.Title = param.Name;
                messageDb.Phone = param.Phone;
                messageDb.Addtime = DateTime.Now;
                messageDb.Status = "0";
                messageDb.Type = "ExampleApply";

                session.Save(messageDb);

                //设置返回数据，默认 ErrorCode 为 0000
                result.Result = new ExampleApplyResultData() { Addtime = messageDb.Addtime };
            }
            else
            {
                result.ErrorCode = "0001";
            }
            return result;
        }

        protected override void PrepareExecute()
        {
            //在这里添加执行命令前的操作

            //是否需要提交
            NeedFlush = true;
        }

        protected override void AfterExecute()
        {
            //在这里添加执行命令后的操作
        }
    }
}
